package com.jwds.digital.bios.business.service.rule.impl;

import com.jwds.digital.bios.business.core.constants.RuleCodeConstant;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchParameterReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.FiveBasicInformationReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.flow.FlowBasicReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.FlowBasicRespVO;
import com.jwds.digital.bios.business.service.rule.DispatchRuleSortInterface;
import com.jwds.digital.bios.business.service.service.flow.IFlowBasicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* 根据流程的能耗和距离
* */
@Slf4j
@Component
public class RouteEnergyConsumption implements DispatchRuleSortInterface {

    @Autowired
    IFlowBasicService iFlowBasicService;
    @Override
    public String getRuleCode() {
        return RuleCodeConstant.ROUTE_ENERGY_CONSUMPTION;
    }

    @Override
    public List<DispatchParameterReqDTO> sortProcess(List<DispatchParameterReqDTO> dataList) {
        List<DispatchParameterReqDTO> resultList = new ArrayList<>();
        List<FiveBasicInformationReqDTO> flowList = new ArrayList<>();
        FlowBasicReqDTO flowBasicReqDTO = new FlowBasicReqDTO();
        List<FlowBasicRespVO> list = new ArrayList<>();
        if (dataList != null){
            for (DispatchParameterReqDTO reqDTO : dataList) {
                if (reqDTO.getFlowList()!=null&&reqDTO.getSlot()!=null&&reqDTO.getDeviceCodeList()!=null&&reqDTO.getSlotIdentification()!=null){
                    for (FiveBasicInformationReqDTO fiveBasicInformationReqDTO : reqDTO.getFlowList()) {
                        if (fiveBasicInformationReqDTO.getOtherSlotList()!=null&&fiveBasicInformationReqDTO.getFlow()!=null&&fiveBasicInformationReqDTO.getDeviceCodeList()!=null){
                            BeanUtils.copyProperties(fiveBasicInformationReqDTO.getFlow(),flowBasicReqDTO);
                            list.addAll(iFlowBasicService.list(flowBasicReqDTO)) ;
                        }else {
                            log.info("流程传过来数据为空");
                        }
                    }
                }else {
                    log.info("流程传过来数据为空");
                }
            }
        }else {
            log.info("流程传过来数据为空");
        }
        List<FlowBasicRespVO> collect=new ArrayList<>();
        collect.addAll(list.stream().sorted(Comparator.comparing(FlowBasicRespVO::getFlowRatedPower)).collect(Collectors.toList()));
        for (DispatchParameterReqDTO reqDTO : dataList) {
            DispatchParameterReqDTO dispatchConfiguratReqDTO = new DispatchParameterReqDTO();
            dispatchConfiguratReqDTO.setSlot(reqDTO.getSlot());
            dispatchConfiguratReqDTO.setDeviceCodeList(reqDTO.getDeviceCodeList());
            dispatchConfiguratReqDTO.setSlotIdentification(reqDTO.getSlotIdentification());
            dispatchConfiguratReqDTO.setDeviceCodeDefault(reqDTO.getDeviceCodeDefault());
            dispatchConfiguratReqDTO.setState(reqDTO.getState());
            for (FiveBasicInformationReqDTO fiveBasicInformationReqDTO : reqDTO.getFlowList()) {
                for (FlowBasicRespVO flowBasicRespVO : collect) {
                    FiveBasicInformationReqDTO dto =  new FiveBasicInformationReqDTO();
                    dto.setFlow(flowBasicRespVO);
                    dto.setOtherSlotDefault(fiveBasicInformationReqDTO.getOtherSlotDefault());
                    dto.setOtherSlotList(fiveBasicInformationReqDTO.getOtherSlotList());
                    dto.setDeviceCodeList(fiveBasicInformationReqDTO.getDeviceCodeList());
                    dto.setDeviceTypeDefault(fiveBasicInformationReqDTO.getDeviceTypeDefault());
                    flowList.add(dto);
                }
            }
            dispatchConfiguratReqDTO.setFlowList(flowList);
            resultList.add(dispatchConfiguratReqDTO);
        }
        return resultList;
    }
}
